<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>websocket</title>
</head>
<body>
<h1>Websocket echo demo</h1>
<form>
    <p>Message:<input id="msg" type="text" value="Hello world!"></p>
    <div id="rmsg"></div>
</form>
<button onclick="send()">Send Message</button>
<script type="text/javascript">
    var sock = null;
    var wsuri = "ws://127.0.0.1:8998"
    window.onload = function () {
        console.log("onload");
        sock = new WebSocket(wsuri)
        sock.onopen = function () {
            console.log("connected to " + wsuri);
        }
        sock.onmessage = function (e) {
            console.log("message received: " + e.data);
            document.getElementById("rmsg").innerHTML += "<br>" + e.data;
        }
        sock.onclose = function (e) {
            console.log("connection closed (" + e.code + ")");
        }
        sock.onerror = function (e) {
            console.error(e)
        }
    };

    function send() {
        const msg = document.getElementById("msg").value;
        sock.send(msg)
    }
</script>
</body>
</html>